#include "volumetool.h"
#include "ui_volumetool.h"

#include <QGraphicsDropShadowEffect>
#include <QPaintEvent>
#include <QPainter>
#include <QDebug>

VolumeTool::VolumeTool(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::VolumeTool),
    isMuted(false),
    volumeRatio(20)
{
    ui->setupUi(this);

    // 设置为弹出窗口，并去除窗口的边框和阴影效果
    setWindowFlags(Qt::Popup | Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint);

    // 设置窗口背景为透明
    setAttribute(Qt::WA_TranslucentBackground);

    // 给窗口增加自定义的阴影效果
    QGraphicsDropShadowEffect* shadowEffect = new QGraphicsDropShadowEffect(this);
    shadowEffect->setOffset(0, 0);
    shadowEffect->setColor("#646464");
    shadowEffect->setBlurRadius(10);
    this->setGraphicsEffect(shadowEffect);

    // 设置按钮图标
//    ui->silenceBtn->setIcon(QIcon(":/images/volumn.png"));
    ui->silenceBtn->setStyleSheet("#silenceBtn { background-image: url(:/images/volumn.png); } #silenceBtn:hover { background-image: url(:/images/volumn-green.png) }");

    // 将默认的音量设置为20%
    ui->volumeRatio->setText("20%");  // 调整文本
    QRect rect = ui->inLine->geometry();
    rect.setY(rect.height() * (1 - 20 / 100.0) + rect.y());
    ui->outLine->setGeometry(rect);  // 调整滑动条
    QPoint point(ui->sliderBox->x(), rect.y() - ui->sliderBox->height() / 2 + ui->volumeBox->y());
    ui->sliderBox->move(point);  // 调整按钮

    ui->volumeBox->installEventFilter(this);
    ui->sliderBox->installEventFilter(this);
}

VolumeTool::~VolumeTool()
{
    delete ui;
}

void VolumeTool::paintEvent(QPaintEvent *event)
{
    (void)event;
    // 绘制VolumeTool界面下的三角
    QPainter painter(this);

    // 设置画笔
    painter.setPen(Qt::NoPen);

    // 设置画刷
    painter.setBrush(QBrush(Qt::white));

    // 绘制三角形
    QPoint datum(this->width() / 2, ui->volumeWidget->y() + ui->volumeWidget->height());
    QPoint a = datum + QPoint(-16, 0);
    QPoint b = datum + QPoint(16, 0);
    QPoint c = datum + QPoint(0, 16);
    QPolygon polygon({a, b, c});
    painter.drawPolygon(polygon);
}

bool VolumeTool::eventFilter(QObject *watched, QEvent *event)
{
    if (ui->volumeBox == watched || ui->sliderBox == watched)
    {
        // 鼠标按下或者鼠标移动时，需要调节音量
        if (event->type() == QEvent::MouseButtonPress || event->type() == QEvent::MouseMove)
        {
            calcVolume();
            emit setMusicVolume(volumeRatio);
        }
        return true;
    }
    return QObject::eventFilter(watched, event);
}

void VolumeTool::calcVolume()
{
    // 获取鼠标点击时的y坐标
    int y = ui->volumeBox->mapFromGlobal(QCursor::pos()).y();

    // 验证y的合法性：[0, 200]
    int start = ui->inLine->y();
    int end = ui->inLine->height() + start;
    y = y > end ? end : y;
    y = y < start ? start : y;

    // 计算音量大小
    volumeRatio = 100 - y * 100 / ui->inLine->height();

    ui->volumeRatio->setText(QString::number(volumeRatio) + QString("%"));  // 调整文本
    QRect rect = ui->inLine->geometry();
    rect.setY(y / 2 * 2 + rect.y());
    ui->outLine->setGeometry(rect);  // 调整outLine滑动条
    QPoint point(ui->sliderBox->x(), rect.y() - ui->sliderBox->height() / 2 + ui->volumeBox->y());
    ui->sliderBox->move(point);  // 调整按钮

    setSilenceBtnImage(volumeRatio == 0);  // 切换静音图标
}

void VolumeTool::setSilenceBtnImage(bool isMuted)
{
    if (isMuted)
    {
        ui->silenceBtn->setStyleSheet("#silenceBtn { background-image: url(:/images/silent.png); } #silenceBtn:hover { background-image: url(:/images/silent-green.png) }");
    }
    else
    {
        ui->silenceBtn->setStyleSheet("#silenceBtn { background-image: url(:/images/volumn.png); } #silenceBtn:hover { background-image: url(:/images/volumn-green.png) }");
    }
}

void VolumeTool::on_silenceBtn_clicked()
{
    isMuted = !isMuted;

    // 切换静音图标
    setSilenceBtnImage(isMuted);

    // 发送信号
    emit setMusicMuted(isMuted);
}
